/* SPDX-License-Identifier: GPL-2.0 */
/**
 * gmac_e.h - Generic definitions of LomboTech GMAC Driver.
 *
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __GMAC_E_H__
#define __GMAC_E_H__

#define LGMAC_DRV_NAME			"lombo gmac"
#define LGMAC_DRV_VERSION		"lombo gmac driver v0.1"

/* These need to be power of 2, and >= 4 */
#define LGMAC_RX_DESC_CNT		(256)
#define LGMAC_TX_DESC_CNT		(256)
#define LGMAC_GET_ENTRY(x, size)	(((x) + 1) & ((size) - 1))

#define LGMAC_RX_DESC_THRESH		(LGMAC_RX_DESC_CNT / 4)
#define LGMAC_TX_DESC_THRESH		(LGMAC_TX_DESC_CNT / 4)

/* Rx Buffer Size must be < 8191 and multiple of 4/8/16 bytes */
#define LGMAC_BUF_SIZE_8K		(8188)
#define LGMAC_BUF_SIZE_4K		(4096)
#define LGMAC_BUF_SIZE_2K		(2048)
#define LGMAC_BUF_SIZE_DEF		(1536)

#define	LGMAC_TSO_MAX_BUF_SIZE		(SZ_16K - 1)

#define LGMAC_LPI_TIMER_MS		(1000)

#define LGMAC_FLOW_CTRL_RX		BIT(0)
#define LGMAC_FLOW_CTRL_TX		BIT(1)
#define LGMAC_FLOW_CTRL_OFF		(0)
#define LGMAC_FLOW_CTRL_BOTH		(LGMAC_FLOW_CTRL_TX | \
					 LGMAC_FLOW_CTRL_RX)

#define LGMAC_FLOW_CTRL_PAUSE		(0xffff)

#define LGMAC_TX_COAL_TIMER_MAX		(100000)
#define LGMAC_TX_COAL_TIMER		(1000)
#define LGMAC_TX_COAL_FRAME_MAX		(256)
#define LGMAC_TX_COAL_FRAME		(1)

#define LGMAC_TX_TIMEOUT_MS		(5000)

#define LGMAC_MAX_OUTSTAND_LIMIT	(4)
#define LGMAC_DMA_MAX_BURST		(16)

#define LGMAC_MII_BUS_NAME		"lbmii"

enum lgmac_state {
	LGMAC_DOWN,
	LGMAC_RESET_REQUESTED,
	LGMAC_RESETTING,
	LGMAC_SERVICE_SCHED,
};

struct lgmac_tx_info {
	dma_addr_t buf;
	unsigned int len;
	u32 map_as_page:1;
	u32 last_segment:1;
};

struct lgmac_tx_queue {
	u32 tx_count_frames;
	struct timer_list txtimer;
	u32 queue_index;
	struct lombo_gmac *priv_data;
	struct dma_desc *dma_tx ____cacheline_aligned_in_smp;
	struct sk_buff **tx_skbuff;
	struct lgmac_tx_info *tx_skbuff_dma;
	unsigned int cur_tx;
	unsigned int dirty_tx;
	dma_addr_t dma_tx_phy;
	u32 tx_tail_addr;
	u32 mss;
};

struct lgmac_rx_queue {
	u32 queue_index;
	struct lombo_gmac *priv_data;
	struct dma_desc *dma_rx ____cacheline_aligned_in_smp;
	struct sk_buff **rx_skbuff;
	dma_addr_t *rx_skbuff_dma;
	unsigned int cur_rx;
	unsigned int dirty_rx;
	u32 rx_zeroc_thresh;
	dma_addr_t dma_rx_phy;
	u32 rx_tail_addr;
};

struct lgmac_channel {
	struct napi_struct napi ____cacheline_aligned_in_smp;
	struct lombo_gmac *priv_data;
	u32 index;
	int has_rx;
	int has_tx;
};

struct lombo_gmac {
	void __iomem		*base_addr;

	struct clk		*clk_gate;
	struct clk		*clk_reset;
	struct clk		*clk_axi;
	struct clk		*clk_parent;
	struct clk		*clk_core;
	struct clk		*clk_sclk0;
	struct clk		*clk_sclk1;

	struct device		*dev;
	struct net_device	*net_dev;

	struct mutex		lock;		/* Consider about spinlock */

	u32			tx_queues_to_use;
	u32			rx_queues_to_use;

	unsigned long		state;
	struct workqueue_struct	*workqueue;
	struct work_struct	service_task;

	struct lgmac_tx_queue	tx_queue[LGMAC_TX_QUEUES_MAX];
	struct lgmac_rx_queue	rx_queue[LGMAC_RX_QUEUES_MAX];
	struct lgmac_channel	channel[LGMAC_CH_MAX];

	u32			tso_en;
	u32			flow_ctrl;
	u32			dma_buf_sz;
	u32			rx_wdt_cnt;
	u32			use_riwt;
	u32			rx_csum;

	phy_interface_t		phy_interface;
	struct phy_device	*phy_dev;
	struct mii_bus		*mii_bus;
	int			old_link;
	int			old_speed;
	int			old_duplex;
	int			phy_addr;
	int			phy_rst_pin;
	int			phy_rst_level;

	u32			tx_delay[3];
	u32			rx_delay[3];
	u32			rmii_osc_ext;

	u32			tx_coal_frames;
	u32			tx_coal_timer;

	int			eee_enable;
	int			eee_active;
	u32			tx_lpi_enter;
	u32			tx_lpi_time;
	struct timer_list	eee_ctrl_timer;

	struct workqueue_struct	*workqueue_phy_reset;
	struct work_struct	phy_reset;
};

#endif /* __GMAC_E_H__ */
